找传奇、传世资源到传世资源站!

android_省市县三级联动_实例源码(含sqlite数据库)下载_

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

android版的省市三级联动,使用sqllite本地数据,数据库在这个目录下 res\raw

android_省市县三级联动_实例源码(含sqlite数据库)下载_ Android平台开发-第1张

package cc.lifelink.cn;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Toast;


public class City_cnActivity extends Activity {
  /** Called when the activity is first created. */
	private DBManager dbm;
	private SQLiteDatabase db;
	private Spinner spinner1 = null;
	private Spinner spinner2=null;
	private Spinner spinner3=null;
	private String province=null;
	private String city=null;
	private String district=null;
	
	
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    spinner1=(Spinner)findViewById(R.id.spinner1);
    spinner2=(Spinner)findViewById(R.id.spinner2);
    spinner3=(Spinner)findViewById(R.id.spinner3);
		spinner1.setPrompt("省");
		spinner2.setPrompt("城市");		
		spinner3.setPrompt("地区");
		
    initSpinner1();
  }
  
  public void initSpinner1(){
		dbm = new DBManager(this);
	 	dbm.openDatabase();
	 	db = dbm.getDatabase();
	 	List<MyListItem> list = new ArrayList<MyListItem>();
		
	 	try {  
	    String sql = "select * from province"; 
	    Cursor cursor = db.rawQuery(sql,null); 
	    cursor.moveToFirst();
	    while (!cursor.isLast()){ 
		    String code=cursor.getString(cursor.getColumnIndex("code")); 
		    byte bytes[]=cursor.getBlob(2); 
		    String name=new String(bytes,"gbk");
		    MyListItem myListItem=new MyListItem();
		    myListItem.setName(name);
		    myListItem.setPcode(code);
		    list.add(myListItem);
		    cursor.moveToNext();
	    }
	    String code=cursor.getString(cursor.getColumnIndex("code")); 
	    byte bytes[]=cursor.getBlob(2); 
	    String name=new String(bytes,"gbk");
	    MyListItem myListItem=new MyListItem();
	    myListItem.setName(name);
	    myListItem.setPcode(code);
	    list.add(myListItem);
	    
	  } catch (Exception e) { 
	  } 
	 	dbm.closeDatabase();
	 	db.close();	
	 	
	 	MyAdapter myAdapter = new MyAdapter(this,list);
	 	spinner1.setAdapter(myAdapter);
		spinner1.setOnItemSelectedListener(new SpinnerOnSelectedListener1());
	}
  public void initSpinner2(String pcode){
		dbm = new DBManager(this);
	 	dbm.openDatabase();
	 	db = dbm.getDatabase();
	 	List<MyListItem> list = new ArrayList<MyListItem>();
		
	 	try {  
	    String sql = "select * from city where pcode='" pcode "'"; 
	    Cursor cursor = db.rawQuery(sql,null); 
	    cursor.moveToFirst();
	    while (!cursor.isLast()){ 
		    String code=cursor.getString(cursor.getColumnIndex("code")); 
		    byte bytes[]=cursor.getBlob(2); 
		    String name=new String(bytes,"gbk");
		    MyListItem myListItem=new MyListItem();
		    myListItem.setName(name);
		    myListItem.setPcode(code);
		    list.add(myListItem);
		    cursor.moveToNext();
	    }
	    String code=cursor.getString(cursor.getColumnIndex("code")); 
	    byte bytes[]=cursor.getBlob(2); 
	    String name=new String(bytes,"gbk");
	    MyListItem myListItem=new MyListItem();
	    myListItem.setName(name);
	    myListItem.setPcode(code);
	    list.add(myListItem);
	    
	  } catch (Exception e) { 
	  } 
	 	dbm.closeDatabase();
	 	db.close();	
	 	
	 	MyAdapter myAdapter = new MyAdapter(this,list);
	 	spinner2.setAdapter(myAdapter);
		spinner2.setOnItemSelectedListener(new SpinnerOnSelectedListener2());
	}
  public void initSpinner3(String pcode){
		dbm = new DBManager(this);
	 	dbm.openDatabase();
	 	db = dbm.getDatabase();
	 	List<MyListItem> list = new ArrayList<MyListItem>();
		
	 	try {  
	    String sql = "select * from district where pcode='" pcode "'"; 
	    Cursor cursor = db.rawQuery(sql,null); 
	    cursor.moveToFirst();
	    while (!cursor.isLast()){ 
		    String code=cursor.getString(cursor.getColumnIndex("code")); 
		    byte bytes[]=cursor.getBlob(2); 
		    String name=new String(bytes,"gbk");
		    MyListItem myListItem=new MyListItem();
		    myListItem.setName(name);
		    myListItem.setPcode(code);
		    list.add(myListItem);
		    cursor.moveToNext();
	    }
	    String code=cursor.getString(cursor.getColumnIndex("code")); 
	    byte bytes[]=cursor.getBlob(2); 
	    String name=new String(bytes,"gbk");
	    MyListItem myListItem=new MyListItem();
	    myListItem.setName(name);
	    myListItem.setPcode(code);
	    list.add(myListItem);
	    
	  } catch (Exception e) { 
	  } 
	 	dbm.closeDatabase();
	 	db.close();	
	 	
	 	MyAdapter myAdapter = new MyAdapter(this,list);
	 	spinner3.setAdapter(myAdapter);
		spinner3.setOnItemSelectedListener(new SpinnerOnSelectedListener3());
	}
  
	class SpinnerOnSelectedListener1 implements OnItemSelectedListener{
		
		public void onItemSelected(AdapterView<?> adapterView, View view, int position,
				long id) {
			province=((MyListItem) adapterView.getItemAtPosition(position)).getName();
			String pcode =((MyListItem) adapterView.getItemAtPosition(position)).getPcode();
			
			initSpinner2(pcode);
			initSpinner3(pcode);
		}

		public void onNothingSelected(AdapterView<?> adapterView) {
			// TODO Auto-generated method stub
		}		
	}
	class SpinnerOnSelectedListener2 implements OnItemSelectedListener{
		
		public void onItemSelected(AdapterView<?> adapterView, View view, int position,
				long id) {
			city=((MyListItem) adapterView.getItemAtPosition(position)).getName();
			String pcode =((MyListItem) adapterView.getItemAtPosition(position)).getPcode();

			initSpinner3(pcode);
		}

		public void onNothingSelected(AdapterView<?> adapterView) {
			// TODO Auto-generated method stub
		}		
	}
	
	class SpinnerOnSelectedListener3 implements OnItemSelectedListener{
		
		public void onItemSelected(AdapterView<?> adapterView, View view, int position,
				long id) {
			district=((MyListItem) adapterView.getItemAtPosition(position)).getName();
			Toast.makeText(City_cnActivity.this, province " " city " " district, Toast.LENGTH_LONG).show();
		}

		public void onNothingSelected(AdapterView<?> adapterView) {
			// TODO Auto-generated method stub
		}		
	}
}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复